   package com.javalong.affairs.base.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.javalong.affairs.R;
import com.javalong.affairs.base.common.ISynCallback;
import com.javalong.affairs.base.common.ServiceLocator;
import com.javalong.affairs.base.common.UIHandler;
import com.javalong.affairs.base.constant.Constant;
import com.javalong.affairs.base.dialog.ProgressDialog;
import com.javalong.affairs.base.ui.TopTitle.OnLeftDrawableClick;
import com.javalong.affairs.base.utils.AppUtils;
import com.javalong.affairs.base.utils.ToastUtils;

   public class UIHandlerActivity extends FragmentActivity implements OnClickListener,
		OnLeftDrawableClick, ISynCallback {
	public ProgressDialog progressDialog = null;
	private long lastClickTime = 0;
	public UIHandlerActivity() {
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AppUtils.getInstance().addActivity(this);
		initProgressDialog();
	}

	@Override
	public void onClick(View v) {
	}

	private void initProgressDialog() {
		progressDialog = new ProgressDialog(this, R.style.ProgressDialog);
		// 点击外面不能取消进度条
		progressDialog.setCanceledOnTouchOutside(false);
		Window dialogWindow = progressDialog.getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = 100; // 宽度
		lp.height = 100; // 高度
		dialogWindow.setAttributes(lp);
	}

	@Override
	public void leftClick() {
		finish();
	}

	private int[] events;

	protected void registerEvent(int[] events) {
		this.events = events;
		UIHandler uiHandler = ServiceLocator.GetInstance().getInstance(
				UIHandler.class);
		if (uiHandler != null) {
			for (int i = 0; i < events.length; i++) {
				uiHandler.registerCallback(this, events[i]);
			}
		}
	}

	@Override
	public void OnSynCalled(int ulID, Bundle bundle) {
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		UIHandler uiHandler = ServiceLocator.GetInstance().getInstance(
				UIHandler.class);
		if (events != null) {
			for (int i = 0; i < events.length; i++) {
				uiHandler.unRegisterCallback(this, events[i]);
			}
		}
	}

	public void refresh() {
	}

	public void oneMoreOut() {
		long now = System.currentTimeMillis();
		if (lastClickTime == 0) {
			Toast.makeText(this, Constant.PRESS_LOGOUT, Toast.LENGTH_SHORT)
					.show();
		} else {
			long time = now - lastClickTime;
			if (time < Constant.TIME_INTERVAL) {
				// 退出之前，存入userInfo信息
				AppUtils.getInstance().exit();
			} else {
				ToastUtils.showMessage(Constant.PRESS_LOGOUT);
			}
		}
		lastClickTime = now;
	}
	
}
